﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Lightray.Hattrick.ChppLib.EntityTypes;
using System.Globalization;

namespace Lightray.Hattrick.ChppLib
{
    public class CandidatesParser
    {
        public static List<CandidatePlayer> ParseCandidates(String candidates)
        {
            List<CandidatePlayer> retVal = new List<CandidatePlayer>();
            if (candidates.Contains("Displaying page "))
            {
                candidates = candidates.Substring(candidates.IndexOf("Displaying page ")); // go to search results
                candidates = candidates.Substring(candidates.IndexOf("\r\n") + 2); // go to Next Line
                while (candidates.Contains("No buyer found."))
                {
                    candidates = candidates.Substring(candidates.IndexOf("No buyer found."));
                    candidates = candidates.Substring(candidates.IndexOf("\r\n") + 2); // go to Next Line
                }
                while (candidates.Contains("Sold!"))
                {
                    candidates = candidates.Substring(candidates.IndexOf("Sold!"));
                    candidates = candidates.Substring(candidates.IndexOf("\r\n") + 2); // go to Next Line
                }
                if (candidates.IndexOf("His team is") == 0)
                    candidates = candidates.Substring(candidates.IndexOf("match is finished. ") + "match is finished. ".Length); // go to Next Line
                // now pointing at first "good" player
                while (candidates.Contains("Scoring: "))
                {
                    int playerDataLength = candidates.IndexOf("\r\n", candidates.IndexOf("Scoring: ")) + 2;
                    CandidatePlayer candidate = ParsePlayerData(candidates.Substring(0, playerDataLength));
                    retVal.Add(candidate);
                    candidates = candidates.Substring(playerDataLength);
                }
                candidates = candidates.Substring(candidates.IndexOf("\r\n") + 2); // go to first player
            }
            return retVal;    
        }

        private static CandidatePlayer ParsePlayerData(string data)
        {
            CandidatePlayer retVal = new CandidatePlayer();
            //String teamName;
            string temp;
            int nameEnd = FindFirstDigit(data) - 1;

            temp = data.Substring(1, nameEnd);
            retVal.FirstName = temp.Substring(0, temp.IndexOf(" "));
            retVal.LastName = temp.Substring(temp.IndexOf(" ") + 1).TrimEnd(' ');
            data = data.Substring(nameEnd) + 1; // goto price
            string price = data.Substring(0, data.IndexOf("(") - 1);
            price = price.Replace(" ", "");
            retVal.CurrentBid = int.Parse(price.Substring(0, FindLastDigit(price)));
            data = data.Substring(data.IndexOf("(") + 1);
            retVal.HighestBidder = data.Substring(0, data.IndexOf(")"));
            if (retVal.HighestBidder == "opening price")
                retVal.HighestBidder = "NO BID";
            data = data.Substring(data.IndexOf("\r\n") + 2);
            retVal.Experience = GetSkillValueFromData(ref data, "Has ", " experience");
            retVal.Leadership = GetSkillValueFromData(ref data, "and ", " leadership");
            retVal.Form = GetSkillValueFromData(ref data, "abilities. ", " form");
            retVal.CurrentOwner = GetStringValueFromData(ref data, "to: ", " \r\n");
            int years = GetIntValueFromData(ref data, "Age:  ", " years");
            int days = GetIntValueFromData(ref data, " (", " days) ");
            retVal.Age = new HattrickAge(years, days);
            retVal.TSI = GetIntValueFromData(ref data, "TSI:  ", " \r\n");
            String specialty = GetStringValueFromData(ref data, "Specialty:  ", " \r\n");
            retVal.Specialty = (Specialty)Enum.Parse(typeof(Specialty), specialty.Contains("-")? "None" : specialty);
            retVal.DeadLine = DateTime.Parse(GetStringValueFromData(ref data, "Deadline:  ", "  \r\n"));
            retVal.Stamina = GetSkillValueFromData(ref data, "Stamina:  ", "  Keeper");
            retVal.Keeper = GetSkillValueFromData(ref data, "Keeper:  ", "  \r\n");
            retVal.Playmaking = GetSkillValueFromData(ref data, "Playmaking:  ", "  Passing:");
            retVal.Passing = GetSkillValueFromData(ref data, "Passing:  ", "  \r\n");
            retVal.Winger = GetSkillValueFromData(ref data, "Winger:  ", "  Defending");
            retVal.Defending = GetSkillValueFromData(ref data, "Defending:  ", "  \r\n");
            retVal.Scoring = GetSkillValueFromData(ref data, "Scoring:  ", "  Set pieces");
            retVal.SetPieces = GetSkillValueFromData(ref data, "Set pieces:  ", "  \r\n");
            return retVal;
        }

        private static SkillLevel GetSkillValueFromData(ref string data, String before, String after)
        {
            data = data.Substring(data.IndexOf(before) + before.Length);
            String value = FirstCharToUpper(data.Substring(0, data.IndexOf(after)));
            if (value.Contains(" "))
                return SkillLevel.WorldClass;
            if (value.Contains("Extra"))
                return SkillLevel.ExtraTerrestrial;
            if (value.Contains("Non-"))
                return SkillLevel.NonExistent;
            return ((SkillLevel)Enum.Parse(typeof(SkillLevel), value));
        }
        private static String GetStringValueFromData(ref string data, String beforeData, String afterData)
        {
            data = data.Substring(data.IndexOf(beforeData) + beforeData.Length);
            String value = data.Substring(0, data.IndexOf(afterData));
            data = data.Substring(data.IndexOf(afterData) + afterData.Length);
            return value;
        }
        private static int GetIntValueFromData(ref string data, String beforeData, String afterData)
        {
            String value = GetStringValueFromData(ref data, beforeData, afterData);
            value = value.Replace(" ", "");
            return int.Parse(value);
        }

        private static string FirstCharToUpper(string p)
        {
            return char.ToUpper(p[0]) + p.Substring(1);
        }

        private static int FindLastDigit(string text)
        {
            int i = 0;
            while (i < text.Length)
            {
                if (char.IsDigit(text[i]))
                    i++;
                else
                    return i;
            }
            return text.Length;
        }
        private static int FindFirstDigit(string data)
        {
            int i = 0;
            while (i < data.Length)
            {
                if (char.IsDigit(data[i]))
                    return i;
                else
                    i++;
            }
            return data.Length;
        }
    }
}
